﻿using System;
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace PrintDll
{
    public class OPrintHelper
    {
        public static void Print(UserControl userControl, string description = "ccc")
        {
            //if (ParamClass.PrintType == 0)
            //    PrintNormal(userControl, description);
            //else
            //PrintRorate(userControl, description);
            PrintRorate(userControl, description);
        }

        /// <summary>
        /// 旋转为横向打印
        /// </summary>
        /// <param name="userControl"></param>
        /// <param name="description"></param>
        private static void PrintRorate(UserControl userControl, string description)
        {
            string msg = "";
            //var rotateTransform = new RotateTransform(180);
            //msg += "转90度;";
            //userControl.LayoutTransform = rotateTransform;
            //刷新UI
            var size = new Size(userControl.ActualHeight, userControl.ActualWidth);
            userControl.Measure(size);
            msg += "大小ActualHeight" + userControl.ActualHeight.ToString() + "ActualWidth" + userControl.ActualWidth.ToString() + ";";
            userControl.Arrange(new Rect(new Point(0, 0), size));
            //设置对齐方式
            userControl.HorizontalAlignment = HorizontalAlignment.Center;
            userControl.VerticalAlignment = VerticalAlignment.Top;
            msg += "HorizontalAlignment.Left;";
            msg += "VerticalAlignment.Top;";

            //更新布局
            userControl.UpdateLayout();
            //参数


            saveMsg(msg);
            //开始打印
            PrintNormal(userControl, description);

            //var paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
            //printDialog.PrintDocument(paginator, description);

        }

        private static void saveMsg(string msg)
        {
            if (!System.IO.Directory.Exists("D://签字//"))
            {
                System.IO.Directory.CreateDirectory("D://签字//");
            }
            //将图片保存下来--并且保存Base64字符串
            string nowTimeStr = DateTime.Now.ToString("yyyyMMddHHmmss");
            using (System.IO.StreamWriter sw = System.IO.File.AppendText("D://签字//" + nowTimeStr + "_图片数据Base64.txt"))
            {
                sw.WriteLine(msg);
                sw.Flush();
                sw.Close();
                sw.Dispose();
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="userControl">要打印的控件</param>
        /// <param name="description">打印描述</param>
        public static void PrintNormal(UserControl userControl, string description)
        {
            //var localPrintServer = new LocalPrintServer();
            //var printQueue = localPrintServer.DefaultPrintQueue;
            //if (printQueue.IsInError)
            //{
            //    throw new Exception("打印机处于错误状态");
            //}
            //var printDialog = new PrintDialog
            //{
            //    PrintQueue = printQueue, //打印队列
            //    PrintTicket = { CopyCount = copyCount } //打印个数
            //};
            //设置纸张大小
            //var pageWidth = (int)Math.Ceiling(printDialog.PrintableAreaWidth);
            //var pageHeight = (int)Math.Ceiling(printDialog.PrintableAreaHeight);
            //printDialog.PrintTicket.PageMediaSize = new PageMediaSize(pageWidth, pageHeight);

            PrintDialog printDialog = new PrintDialog();
            //设置纸张大小
            //var pageWidth = (int)Math.Ceiling(printDialog.PrintableAreaWidth); //小标签：114
            //var pageHeight = (int)Math.Ceiling(printDialog.PrintableAreaHeight); //小标签：227
            //printDialog.PrintTicket.PageMediaSize = new PageMediaSize(600, 80);
            printDialog.PrintVisual(userControl, description);

            //var paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
            ////var paginator = ((IDocumentPaginatorSource)document).DocumentPaginator;
            //printDialog.PrintDocument(paginator, description);
        } 
    }
 }